//
//  UserViewController.m
//  WWLNetease
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "UserViewController.h"
#import "WZXAboutOurViewController.h"
#import "WZXActiveWebViewController.h"
#import "QRCodeReaderViewController.h"
#import "WJYMyWebViewController.h"
#import<MessageUI/MFMailComposeViewController.h>// 邮件的头文件


@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, WZXActiveWebViewControllerDelegate, QRCodeReaderDelegate,  MFMailComposeViewControllerDelegate>

@property (nonatomic ,strong)UIImageView *userImageV;

@property (nonatomic ,strong)UIButton *userButton;

@property (nonatomic ,strong)UITableView *myTableView;

@property (nonatomic ,strong)UIScrollView *bigScrollerV;

@property (nonatomic, retain)QRCodeReaderViewController *reader;

@end

@implementation UserViewController


- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0.816 alpha:1.000];
    
#pragma mark - 创建一个大的scrollerView
    self.bigScrollerV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * SIZE, 667 * SIZE)];
    self.bigScrollerV.contentSize = CGSizeMake(0, 680 * SIZE);
    self.bigScrollerV.delegate =self;
#pragma mark - 是否显示垂直滚动条
    self.bigScrollerV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.bigScrollerV];
    
    
    
#pragma mark - 创建最上面大图片
    self.userImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"linshibigView.jpg"]];
    self.userImageV.frame = CGRectMake(0, -20 * SIZE, self.view.frame.size.width, 280 * SIZE);
    self.userImageV.backgroundColor = [UIColor colorWithRed:0.722 green:0.090 blue:0.133 alpha:1.000];
    [self.bigScrollerV addSubview:self.userImageV];
    
#pragma mark - 创建四个button
//    NSArray *btnTitleArr = [NSArray arrayWithObjects:@"历史",@"收藏",@"跟帖",@"金币", nil];
//    NSArray *btnImageNameArr = [NSArray arrayWithObjects:@"user_read_icon@2x.png",@"user_favor_icon@2x.png",@"user_comment_icon@2x.png",@"user_coin_icon@2x.png", nil];
// 
//    for ( int i = 0; i < 4; i++) {
//        
//        self.userButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        self.userButton.frame = CGRectMake(92.5 * SIZE * i + 2.5 * SIZE, 220 * SIZE, 92 * SIZE, 93 * SIZE);
//        self.userButton.backgroundColor = [UIColor whiteColor];
//        [self.userButton setTitle:btnTitleArr[i] forState:UIControlStateNormal];
//        //设置image在button上的位置（上top，左left，下bottom，右right）
//        self.userButton.titleEdgeInsets = UIEdgeInsetsMake(35 * SIZE, -25 * SIZE, 0, 0);
//        [self.userButton setImage:[UIImage imageNamed:btnImageNameArr[i]] forState:UIControlStateNormal];
//        self.userButton.imageEdgeInsets = UIEdgeInsetsMake(-20 * SIZE, 30 * SIZE, 8 * SIZE, 20 * SIZE);
//        self.userButton.titleLabel.font = [UIFont systemFontOfSize:14 * SIZE];//title字体大小
//        [self.userButton setTitleColor:[UIColor colorWithRed:0.631 green:0.635 blue:0.631 alpha:1.000] forState:UIControlStateNormal];
//        self.userButton.tag = 100 + 1;
//        [self.bigScrollerV addSubview:self.userButton];
//        
//    }
    
#pragma mark - 创建一个高度为1的View
    
    UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 280 * SIZE, 375 * SIZE, 2 * SIZE)];
    oneView.backgroundColor = [UIColor colorWithRed:0.737 green:0.733 blue:0.757 alpha:1.000];
    [self.bigScrollerV addSubview:oneView];
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 260 * SIZE, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.scrollEnabled = NO;
    [self.bigScrollerV addSubview:self.myTableView];
    
#pragma mark - 注册
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  
}

#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    /* 我的界面里的title数组 */
    NSArray *titleArray = [NSArray arrayWithObjects:@"关于我们",@"清除缓存",@"活动广场",@"二 维 码",@"邮箱反馈", nil];
    /* 我的界面里的title图片数组 */
    NSArray *imageArray = [NSArray arrayWithObjects:@"user_set_icon_message@2x.png", @"user_set_icon_mission@2x.png", @"user_set_icon_promo@2x.png", @"user_set_icon_setting@2x.png", @"user_set_icon_wallet@2x.png",nil];
    
    if (indexPath.section == 0) {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
    }else if(indexPath.section == 1) {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row + 1];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row + 1]];
        
    }else {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row + 3];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row + 3]];
        
    }
    
    return cell;
}

#pragma mark - 必要的方法
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }if (section == 1) {
        
        return 2;
    }
    else{
        
        return 2;
    }
    
}

#pragma mark - 有几个分区
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}


#pragma mark - 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

#pragma mark - header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 9;
}

#pragma mark - footer
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消点击至灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        //关于我们的点击跳转
        WZXAboutOurViewController *aboutVC = [[WZXAboutOurViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
#pragma mark - 创建UIAlertController
    if (indexPath.section == 1 && indexPath.row == 0) {

        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        float cacheSize = size / 1024.0 / 1024.0;
        // 提示框
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存:%.2fMB",cacheSize]  preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
        
        // 创建确定按钮
        //http://3g.163.com/ntes/special/00340BF8/hdlb.html
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //图片缓存清理
            [[SDImageCache sharedImageCache] cleanDisk];
            
            NSLog(@"缓存大小为%.2f",cacheSize);
            [[SDImageCache sharedImageCache]clearDisk];
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];

        // 创建取消按钮
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        
        [alertC addAction:action2];
        
    }
    if (indexPath.section == 1 && indexPath.row == 1)
    {
        WZXActiveWebViewController *webVC = [[WZXActiveWebViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:webVC];
        webVC.Delegate = self;
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    }
#pragma mark --- 二维码
    if (indexPath.section == 2 && indexPath.row == 0)
    {
        NSArray *types = @[AVMetadataObjectTypeQRCode];
        self.reader = [QRCodeReaderViewController readerWithMetadataObjectTypes:types];
        
        //
        self.reader.delegate = self;
        
        //
        [self.reader setCompletionWithBlock:^(NSString *resultAsString) {
            [self dismissViewControllerAnimated:YES completion:^{
                NSLog(@"----------------%@", resultAsString);
                
                if (!resultAsString.length == 0) {
                    WJYMyWebViewController *web = [[WJYMyWebViewController alloc]init];
                    web.url = resultAsString;
                    [self.navigationController pushViewController:web animated:YES];
                }
                
            }];
        }];
        
        [self presentViewController:self.reader animated:YES completion:NULL];

        
    }
#pragma mark --- 邮箱反馈
    if (indexPath.section == 2 && indexPath.row == 1)
    {
//        WJYEmailViewController *emailVC = [[WJYEmailViewController alloc] init];
//        
//        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:emailVC];
//        
//        [self presentViewController:naVC animated:YES completion:^{
//            
//            
//        }];
        
        [self mailButtonAction];
        
    }

    
}

#pragma mark - QRCodeReader Delegate Methods

- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"%@", result);
        
    }];
}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)changeNavigationBarColor
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.886 green:0.227 blue:0.255 alpha:1];
}

#pragma mark - 发邮件按钮方法
- (void)mailButtonAction
{
    
#pragma mark - 方法一: 使用openURL发送邮件
    /*
     NSMutableString *mailUrl = [[NSMutableString alloc]init];
     //添加收件人
     NSArray *toRecipients = @[@"244254135@qq.com", @"444785965@qq.com"];
     // 注意：如有多个收件人，可以使用componentsJoinedByString方法连接，连接符为@","
     [mailUrl appendFormat:@"mailto:%@", toRecipients];
     
     // 多个收件人可以使用componentsJoinedByString方法连接，连接符为@","
     //    [mailUrl appendFormat:@"mailto : %@", [toRecipients componentsJoinedByString:@","]];
     
     //添加抄送
     NSArray *ccRecipients = @[@"544789652@qq.com", @"1256987456@qq.com"];
     [mailUrl appendFormat:@"?cc=%@", ccRecipients];
     
     //添加密送
     NSArray *bccRecipients = @[@"447449296@qq.com"];
     [mailUrl appendFormat:@"&bcc=%@", bccRecipients];
     
     //添加主题
     [mailUrl appendString:@"&subject=主题啦啦啦"];
     //添加邮件内容
     [mailUrl appendString:@"&body=<b>可以不写的内容</b> 内容啦啦啦"];
     
     NSString *email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
     */
    
    
    
#pragma mark - 方法二:使用MFMailComposeViewController发送邮件
    
    // 继承于UINavigationController
    // 创建
    MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
    
    if (![MFMailComposeViewController canSendMail]) {
        
//        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//        self.hud.labelText = @"此设备不支持邮件功能";
//        self.hud.mode = MBProgressHUDModeText;
//        [self.hud hide:YES afterDelay:2];
        
        return;
        
    }
    
    
    // 设置邮件代理
    mailComposeVC.mailComposeDelegate = self;
    //    [mailComposeVC setMailComposeDelegate:self];
    // 设置邮件主题
    [mailComposeVC setSubject:@"我是邮件主题"];
    // 设置收件人
    [mailComposeVC setToRecipients:@[@"244254135@qq.com"]];
    // 设置抄送人
    [mailComposeVC setCcRecipients:@[@"444785965@qq.com"]];
    // 设置密抄送
    [mailComposeVC setBccRecipients:@[@"544789652@qq.com"]];
    
    
    // 设置邮件的正文内容
    NSString *emailContent = @"我是邮件内容";
    // 是否为HTML格式
    [mailComposeVC setMessageBody:emailContent isHTML:NO];
    // 如使用HTML格式，则为以下代码
    //    [mailComposeVC setMessageBody:@"<html><body><p>Hello</p><p>World！</p></body></html>" isHTML:YES];
    
    
    // 添加一张图片
    UIImage *image = [UIImage imageNamed:@"baby.jpg"];
    NSData *imageData = UIImagePNGRepresentation(image);
    //关于mimeType：http://www.iana.org/assignments/media-types/index.html
    [mailComposeVC addAttachmentData:imageData mimeType:@"" fileName:@"baby.jpg"];
    
    //添加一个pdf附件
    NSString *file = [[NSBundle mainBundle] pathForResource:@"推送证书" ofType:@"pdf"];
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mailComposeVC addAttachmentData:pdf mimeType:@"" fileName:@"推送证书.pdf"];
    
    // 弹出邮件发送视图
    [self presentViewController:mailComposeVC animated:YES completion:nil];
    
    
}


#pragma mark - 邮件的代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    // 邮件发送的状态,根据状态判断
    //    MFMailComposeResultCancelled,
    //    MFMailComposeResultSaved,
    //    MFMailComposeResultSent,
    //    MFMailComposeResultFailed
    
    
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"用户取消编辑");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"用户保存邮件");
            break;
        case MFMailComposeResultSent:
            NSLog(@"用户点击发送");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"发送邮件失败 : %@", [error localizedDescription]);
            break;
    }
    
    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
